package com.example.post.controller;

import com.example.post.model.Remark;
import com.example.post.model.ResponseData;
import com.example.post.model.User;
import com.example.post.services.impl.RemarkServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("/api")
@RestController
public class RemarkController {
    @Autowired
    RemarkServiceImpl remarkService;
    @GetMapping("/remarkAll")
    @ResponseBody
    public ResponseData getAllRemarks() {
        List remarkList=remarkService.getAllRemarks();
        ResponseData res;
        if (remarkList.isEmpty()){
            res = new ResponseData(500,"查询出错","");
        }else{
            res = new ResponseData(200,"查询成功",remarkList);
        }
        return res;
    }
    @GetMapping("/remark/{blogId}")
    @ResponseBody
    public ResponseData getRemarkByBlogId(@PathVariable int blogId) {
        List<Remark> remarkList = remarkService.getRemarkByBlogId(blogId);
        ResponseData res;
        if (remarkList.isEmpty()) {
            res = new ResponseData(500, "查询出错", "");
        } else {
            res = new ResponseData(200, "查询成功", remarkList);
        }
        return res;
    }
    @PostMapping("/remark")
    @ResponseBody
    public ResponseEntity<String> addRemark(HttpSession session, @RequestBody Remark remarkRequest) {
        String content = remarkRequest.getContent();
        User user = (User) session.getAttribute("user");
        int blogId = remarkRequest.getBlogId();
        remarkService.addRemark(content, user.getUsername(), blogId);
        return ResponseEntity.status(HttpStatus.OK).body("评论创建成功");
    }
    @DeleteMapping("/remark/{id}")
    @ResponseBody
    public ResponseData deleteRemark(HttpSession session, @PathVariable int id) {
        User user = (User) session.getAttribute("user");
        Remark remark = remarkService.getRemarkById(id);
        if (user == null || remark == null) {
            return new ResponseData(500, "删除失败", null);
        }
        if (user.getId()==1|| user.getUsername().equals(remark.getObserver())) {
            remarkService.deleteRemark(id);
            return new ResponseData(200, "评论删除成功", null);
        } else {
            return new ResponseData(500, "删除失败，权限不足", null);
        }
    }
    @GetMapping("/remarks")
    public ResponseEntity<List<Remark>> getPostsByTitle(@RequestParam String observer) {
        List<Remark> remarks = remarkService.getRemarksByObserver(observer);
        if (remarks.isEmpty()) {

            return null;
        }
        return ResponseEntity.status(HttpStatus.OK).body(remarks);
    }

    @GetMapping("/myRemarks")
    @ResponseBody
    public ResponseData getPostsByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Remark> remarkList = remarkService.getRemarksByObserver(user.getUsername());

        ResponseData res;
        if (remarkList.isEmpty()){
            res = new ResponseData(500,"查询出错","");
        }else{
            res = new ResponseData(200,"查询成功",remarkList);
        }
        return res;
    }
}
